<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>患者管理</title>
    <title>vue-demo1</title>
    <script src="/js/vue.js"></script>
    <link rel="stylesheet" href="/css/index.css">
    <script src="/js/index.js"></script>
    <script src="/js/axios.min.js"></script>
</head>
<body>
<div id="app">
    <!--查询-->
    <el-row>
        <el-col :span="4"><el-input  v-model="searchForm.patientName"  size="small" placeholder="请输入用户名"></el-input></el-col>
        <el-col :span="4"><el-input v-model="searchForm.idNumber"  size="small" placeholder="请输入身份证号码"></el-input></el-col>
        <el-col :span="4"><el-button type="primary" @click="search" size="small" icon="el-icon-search">搜索</el-button></el-col>
        <el-col :span="4"><el-button type="primary" @click="handlerAdd"  size="small" icon="el-icon-circle-plus">添加</el-button></el-col>
        <el-col :span="4"></el-col>
        <el-col :span="4"></el-col>
    </el-row>
    <!--表格显示-->
    <el-table
            :data="tableData"
            style="width: 100%">
        <el-table-column
                prop="id"
                label="编号"
                width="180">
        </el-table-column>
        <el-table-column
                prop="patientName"
                label="名字"
                width="180">
        </el-table-column>
        <el-table-column
                prop="sex"
                label="性别"
                width="180">
            <template slot-scope="scope">
                <el-tag
                        :type="scope.row.sex == 0 ? 'success' : 'danger'"
                        disable-transitions>{{scope.row.sex == 0 ? "男" : "女"}}
                </el-tag>
            </template>
        </el-table-column>
        <el-table-column
                prop="phone"
                label="手机号"
                width="180">
        </el-table-column>
        <el-table-column
                prop="idNumber"
                label="身份证号">
        </el-table-column>
        <el-table-column
                prop="userName"
                label="用户名">
        </el-table-column>
        <el-table-column label="操作">
            <!--slot槽   scope所有表数据   scope.$index 行下标 scope.row 表中的一行数据-->
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[2,5,10,15,20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>
    <!--更新对话框-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form :model="form" :rules="rules">
            <el-form-item label="名字" :label-width="formLabelWidth" prop="patientName">
                <el-input v-model="form.patientName" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="性别" :label-width="formLabelWidth">
                <!--<el-input v-model="form.sex" autocomplete="off"></el-input>-->
                <el-select v-model="form.sex" placeholder="请选择性别">
                    <el-option label="男" value="0"></el-option>
                    <el-option label="女" value="1"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="手机号" :label-width="formLabelWidth" prop="phone">
                <el-input v-model="form.phone" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="身份证号" :label-width="formLabelWidth" prop="idNumber">
                <el-input v-model="form.idNumber" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="用户名" :label-width="formLabelWidth" prop="userName">
                <el-input v-model="form.userName" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addOrUpdate">确 定</el-button>
        </div>
    </el-dialog>

</div>
<script>
    var app = new Vue({
        el:"#app",
        //存放所有js对象的  plain  javascript object
        data(){
            return{
                tableData: [], //表格数据
                currentPage: 1,//当前页 第几页
                pageSize: 5,//每页显示数量
                total: 400,//总条数
                searchForm:{patientName:'',idNumber:''},//搜索对象
                title: '用户添加', //弹出框标题
                dialogFormVisible: false,//对话框是否显示
                form: { //form表单对象
                    patientName: '',
                    sex: '',
                    phone:'',
                    idNumber:'',
                    userName:''
                },
                rules:{
                    patientName: [
                        { required: true, message: '请输入用户姓名(必填项)', trigger: 'blur' }
                    ],
                    //手机验证
                    phone: [
                        {required: true, message: '请输入手机号码(必填项)', trigger: 'blur'},
                        {pattern: /^1[3456789]\d{9}$/, message: '请输入正确的11位手机号码', trigger: ['blur', 'change']}
                    ],
                    idNumber:[
                        {required: true, message: '请输入身份证号码(必填项)', trigger: 'blur'},
                        {pattern:  /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(([0-2][1-9])|10|20|30|31)\d{3}(\d|X|x)$/, message: '请输入正确的身份证号码', trigger: ['blur', 'change']}
                    ],
                    userName: [
                        { required: true, message: '请输入用户名(必填项)', trigger: 'blur' }
                    ]
                },
                formLabelWidth: '120px' //form中label的宽度
            }
        },
        //所有页面元素触发的事件的方法
        methods:{
            //封装的加载数据方法
            loadData(){
                //定义一个变量，赋值为当前vue对象
                var _this = this;
                //使用axios向后台发请求，获取数据
                //axios.post('http://172.16.7.66:8080/dept/queryByPage', {
                axios.post(//请求方式，必须和后端资源暴露方式一致
                    '/patient/queryByPage',//请求的url
                    {"pageNo":this.currentPage,"pageSize":this.pageSize,"data":{"patientName":this.searchForm.patientName,
                           "idNumber":this.searchForm.idNumber}})//请求携带的参数
                    .then(function (response) {//成功响应的处理方法   response 是响应的数据
                        console.log(response);
                        //response 一定是json对象  stringify 把json对象转json字符串
                        // alert(JSON.stringify(response));
                        //判断返回是否正确
                        if(response.data.code==200){
                            //直接使用this 代表axios的对象  this
                            _this.tableData=response.data.data.list;
                            _this.total=response.data.data.total;
                        }else{
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            //处理每页数量发生变化
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                //让每页数量发生改变
                this.pageSize=val;
                //调用加载数据方法
                this.loadData();
            },
            //处理当前页（页码 第几页）发生变化
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                //让第几页发生改变
                this.currentPage=val;
                //调用加载数据方法
                this.loadData();
            },
            //按条件查询
            search(){
                //直接调用loadData
                this.loadData();
            },
            //处理添加
            handlerAdd(){
                //修改标题
                this.title='用户添加';
                //清空原来的值
                this.form={};
                //让对话框显示
                this.dialogFormVisible = true;
            },
            //处理更新
            handleEdit(index, row) {
                console.log(index, row);
                this.title='用户更新';
                this.form=row;
                //alert(JSON.stringify(this.form));
                //让对话框显示
                this.dialogFormVisible = true;
            },
            //调用接口添加或者更新数据
            addOrUpdate(){
                var url = "/patient/add";
                //判断是否添加还是更新
                if(this.title==='用户更新'){
                    url = "/patient/update";
                }
                //定义一个变量，赋值为当前vue对象
                var _this = this;
                //使用axios向后台发请求，获取数据
                //axios.post('http://172.16.7.66:8080/dept/queryByPage', {
                axios.put(//请求方式，必须和后端资源暴露方式一致
                    url,//请求的url
                    this.form)
                    .then(function (response) {//成功响应的处理方法   response 是响应的数据
                        console.log(response);
                        //response 一定是json对象  stringify 把json对象转json字符串
                        // alert(JSON.stringify(response));
                        //判断返回是否正确
                        if(response.data.code==200){
                            //提示信息
                            _this.$message({
                                message: '恭喜你，这是一条成功消息',
                                type: 'success'
                            });
                            //关闭对话框
                            _this.dialogFormVisible = false;
                            //重新加载数据
                            _this.loadData();
                        }else{
                            _this.$message.error('错了哦，这是一条错误消息');
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            //处理删除
            handleDelete(index, row) {
                if(confirm("是否确定要删除")){
                    axios.delete("/patient/delete", {params:{id:row.id}})
                        .then(response=> {//成功响应的处理方法   response 是响应的数据
                            if(response.data.code==200){
                                //提示信息
                                this.loadData();
                                this.$message.success('恭喜你，这是一条成功消息');
                                // //关闭对话框
                                // _this.dialogFormVisible = false;
                                //重新加载数据

                            }else{
                                console.log(error);
                                this.$message.error('错了哦，这是一条错误消息');
                            }
                        })
                }
            }
        },
        created(){//初始化操作
            //alert("创建后----");
            this.loadData()

        },
    })
</script>
</body>
</html>
